<?php

namespace app\admin\controller\user;

use app\BaseController;
use think\facade\Db; // 数据库的链接

class Cardrecord extends BaseController
{
    public function getlist()
    {
        $get = input();
        // 分页
        $pageSize = $get['pageSize'] > 100 ? 100 : $get['pageSize'];
        $page = $get['pageNo'];
        // 时间段查询 开始时间 和 结束时间
        if (!empty(@$get['time'])) {

            // 开始时间 转时间戳
            $get['time'][0] = strtotime($get['time'][0]);
            // 结束时间 转时间戳
            $get['time'][1] = strtotime($get['time'][1]);

            $sql[] = ['a.obtain_time', 'between', [$get['time'][0], $get['time'][1]]];
        }
        // 查询条件 模糊查询
        if (!empty(@$get['name'])) {
            $sql[] = ['b.name', 'LIKE', "%" . $get['name'] . "%"];
        }
        // 使用状态查询
        if (!empty(@$get['is_status'])) {
            $sql[] = ['a.is_status', '=', $get['is_status'] - 1];
        }
        // 收益状态查询
        if (!empty(@$get['del'])) {
            $sql[] = ['a.del', '=', $get['del'] - 1];
        }
        // 手机号查询
        if (!empty(@$get['phone'])) {
            $sql[] = ['c.phone', '=', $get['phone']];
        }

        // 使用table分页查询数据表wechart_customer_info
        $list = Db::name('user_card_record')
        ->alias('a')
        ->join('card b', 'a.cid = b.id')
        ->join('user c', 'a.uid = c.id')
        ->field("a.*,b.name as card_name,c.phone,FROM_UNIXTIME(a.obtain_time,'%Y-%m-%d %H:%i:%s') as obtain_time,FROM_UNIXTIME(a.usage_time,'%Y-%m-%d %H:%i:%s') as usage_time,FROM_UNIXTIME(a.end_time,'%Y-%m-%d') as end_time")
        ->where(@$sql)
        ->order('a.obtain_time DESC')
        ->paginate([
            'list_rows' => $pageSize,
            'page' => $page
        ])
        ->toArray();

        // 返回数据
        return array(
            'code' => 200,
            'msg' => 'success',
            'data' => $list,
        );
    }
}